import { excludesRouterName, tokenName } from '@/server/base'
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'


import Layout from '@/layout/index.vue';

const routes: RouteRecordRaw[] = [

  {
    path: "/",
    name: "homePage",

    redirect: '/redirectedPage',
  },
  {
    path: "/layout",
    name: "main",
    component: () => import('@/layout/index.vue'),// Layout,
    meta: {
      handler: -1
    },
    children: [
     
      {
        path: "/dataManage",
        name: "dataManage",
        redirect: "/collectionManage",
        // component: () => import("@/views/dataManage/index.vue"),
        meta: {
          label: "数据管理",
          handler: -1,
          // hidden: false,
          elIcon: 'DataAnalysis'
        },
        children: [
          {
            path: "/collectionManage/:projectId?",
            name: "CollectionManage",
            component: () => import("@/views/dataManage/index.vue"),
            meta: {
              label: "数据集管理",
              hidden: false,
              elIcon: 'TrendCharts'
            }
          },
          {
            path: "/taggingManage/:projectId?",
            name: "taggingManage",
            component: () => import("@/views/taggingManage/index.vue"),
            meta: {
              label: "标注管理",
              hidden: false,
              elIcon: 'DocumentChecked'
            }
          },
        ]
      },
      {
        path:"/reasoning",
        name:'reasoning',
        component:()=> import('@/views/reasoning/index.vue'),
        meta:{
          label:"推理设置",
          hidden: true,
          elIcon: 'TrendCharts'
        }
      },
        {
          path:'/editDataStrengthen',
          name:'EditDataStrengthen',
          component:()=> import('@/views/dataStrengthen/editDataStrengthen.vue'),
          meta:{
            label:"新建增强任务",
            hidden: true,
            elIcon: 'TrendCharts'
          }
        },
      {
        path: '/modifyCollection/:isEdit/:value?',
        name: 'modifyCollection',
        component: () => import('@/views/dataManage/modifyCollection.vue'),
        meta: {
          label: '数据集编辑',
          hidden: true,
          elIcon: 'TrendCharts'
        }
      },
      {
        path: '/importCollection/:isEdit/:value?',
        name: 'importCollection',
        component: () => import('@/views/dataManage/importCollection.vue'),
        meta: {
          label: '导入数据集',
          hidden: true,
          elIcon: 'Notebook'
        }
      },
      {
        path: "/tagManage",
        name: "tagManage",
        component: () => import("@/views/tagManage/group.vue"),
        meta: {
          label: "标签管理",
          hidden: false,
          elIcon: 'Paperclip'
        }
      },

      {
        path: "/tag/:value",
        name: "tag",
        component: () => import("@/views/tagManage/tag.vue"),
        meta: {
          label: "标签管理",
          hidden: true,
          elIcon: 'Edit'
        }
      },
      {
        path: '/moreTask',
        name: 'moreTask',
        component: () => import("@/views/moreTask/index.vue"),
        meta: {
          label: "多人任务",
          hidden: false,
          elIcon: 'Monitor'
        }
      },
      {
        path: '/myTaskEdit/:isEdit',
        name: 'myTaskEdit',
        component: () => import("@/views/moreTask/createdTask.vue"),
        meta: {
          label: '创建多人任务',
          hidden: true,
          elIcon: 'Menu',
          width: '80%'
        }
      },



      {
        path: "/dataWash",
        name: "DataWash",
        component: () => import("@/views/dataWash/index.vue"),
        meta: {
          label: "数据清洗",
          hidden: false,
          elIcon: 'Notebook'
        }
      },
      {
        path: "/dataMark",
        name: "DataMark",
        component: () => import("@/views/dataMark/index.vue"),
        meta: {
          label: "数据标注",
          hidden: false,
          elIcon: 'Notebook'
        },
        children: [
          {
            path: "/artificialMark",
            name: "ArtificialMark",
            component: () => import("@/views/artificialMark/index.vue"),
            meta: {
              label: "人工标注",
              hidden: false,
              elIcon: 'Notebook'
            }
          },
          {
            path: "/automaticMark",
            name: "AutomaticMark",
            component: () => import("@/views/automaticMark/index.vue"),
            meta: {
              label: "自动标注",
              hidden: false,
              elIcon: 'Notebook'
            },
            children:[
              
            ]
          }
        ]
      },
      {
        path: "/dataCheck",
        name: "DataCheck",
        component: () => import("@/views/dataCheck/index.vue"),
        meta: {
          label: "数据检查",
          hidden: false,
          elIcon: 'Notebook'
        }
      },
      {
        path: "/dataStrengthen",
        name: "DataStrengthen",
        component: () => import("@/views/dataStrengthen/index.vue"),
        meta: {
          label: "数据增强",
          hidden: false,
          elIcon: 'Notebook'
        }
      },
      {
        path: "/dataDerive",
        name: "DataDerive",
        component: () => import("@/views/dataDerive/index.vue"),
        meta: {
          label: "数据导出",
          hidden: false,
          elIcon: 'Notebook'
        }
      },

      {
        path: '/systemManage',
        name: 'systemManage',
        // component:()=>import("@/views/systemManage/index.vue"),
        meta: {
          label: '系统设置',
          hidden: false,
          elIcon: 'Setting',
          handler: -1
        },
        children: [
          {
            path: 'menuSetting',
            name: 'menuSetting',
            component: () => import("@/views/systemManage/menu/index.vue"),
            meta: {
              label: '菜单设置',
              hidden: false,
              elIcon: 'Menu'
            }
          },
          {
            path: 'menuEdit/:isEdit/:id?',
            name: 'menuEdit',
            component: () => import("@/views/systemManage/menu/edit.vue"),
            meta: {
              label: '菜单编辑',
              hidden: true,
              elIcon: 'Menu',
              customLabel: true,
              width: '800px'
            }
          },
          {
            path: 'roleSetting',
            name: 'roleSetting',
            component: () => import("@/views/systemManage/role/index.vue"),
            meta: {
              label: '角色设置',
              hidden: false,
              elIcon: 'Menu'
            }
          },
          {
            path: 'roleEdit/:isEdit/:id?',
            name: 'roleEdit',
            component: () => import("@/views/systemManage/role/edit.vue"),
            meta: {
              label: '角色编辑',
              hidden: true,
              elIcon: 'Menu',
              customLabel: true,
              width: '800px'
            }
          },
          {
            path: 'teamSetting',
            name: 'teamSetting',
            component: () => import("@/views/systemManage/team/index.vue"),
            meta: {
              label: '团队设置',
              hidden: false,
              elIcon: 'Menu',
              width:'500px'
            }
          },
          {
            path: 'teamEdit/:isEdit/:id?',
            name: 'teamEdit',
            component: () => import("@/views/systemManage/team/edit.vue"),
            meta: {
              label: '团队编辑',
              hidden: true,
              elIcon: 'Menu',
              customLabel: true,
              width: '700px'
            }
          },
          {
            path: 'teamPersonEdit/:isEdit/:id?',
            name: 'teamPersonEdit',
            component: () => import("@/views/systemManage/team/editPerson.vue"),
            meta: {
              label: '团队成员编辑',
              hidden: true,
              elIcon: 'Menu',
              customLabel: true,
              width: '800px'
            }
          },
          {
            path: 'roleMenuCheck/:row',
            name: 'roleMenuCheck',
            component: () => import("@/views/systemManage/role/roleMenuCheck.vue"),
            meta: {
              label: '菜单选择',
              hidden: true,
              elIcon: 'Menu',
              customLabel: true,
              width: '800px'
            }
          }
        ]
      },
      {
        name: 'projectManage',
        path: 'projectMange',
        component: () => import('@/views/projectManage/index.vue'),
        meta: {
          label: '项目管理',
          elIcon: 'Tickets',
        }
      },
      {
        name: 'projectEdit',
        path: 'projectEdit',
        component: () => import('@/views/projectManage/edit.vue'),
        meta: {
          label: '菜单选择',
          hidden: true,
          elIcon: 'Menu',
          customLabel: true,
          width: '800px'
        }
      },




      /**
      * 弹出层页面
      */

      {
        path: '/dialog',
        name: 'dialog',
        // component: () => import('@/layout/dialogPage.vue'),
        children: [
          {
            path: 'createdCollection/:isEdit',
            name: 'createdCollection',
            component: () => import('@/views/dataManage/createdCollection.vue'),
            meta: {
              //【不推荐】通过获取路由 isEdit 参数来判断相应显示的标题 。可以设置参数 customLabel: true,后在页面内部自定义处理
              label: (isEdit: boolean) => isEdit ? '数据集编辑' : '数据集添加',
              hidden: true,
              elIcon: 'Notebook',
              width: '1000px'
            }
          },
          {
            path: "tagGroupEdit/:isEdit/:value?",
            name: "tagGroupEdit",
            component: () => import("@/views/tagManage/groupModify.vue"),
            meta: {
              label: "标签组编辑",
              customLabel: true,
              canBack: true,
              hidden: true,
              elIcon: 'Notebook',
              width: '600px'
            }
          },
          {
            path: "tagEdit/:isEdit/:groupId/:value?",
            name: "tagEdit",
            component: () => import("@/views/tagManage/tagModify.vue"),
            meta: {
              label: "标签编辑",
              customLabel: true,
              hidden: true,
              elIcon: 'Notebook',
              width: '600px'
            }
          },
          {
            path: "collectionHistory/:isEdit/:value?",
            name: "collectionHistory",
            component: () => import("@/views/dataManage/collectionHistory.vue"),
            meta: {
              label: "数据导入记录",
              customLabel: true,
              hidden: true,
              elIcon: 'Notebook',
              width: '80%'
            }
          }
        ],
        meta: {
          label: '二级弹出页面',
          hidden: true,
          elIcon: 'Notebook',
        }
      },

    ]
  },

  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      zIndex: 12,
      label: '登录',
      hidden: true
    }
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/views/register/index.vue'),
    meta: {
      zIndex: 13,
      label: '注册',
      hidden: true
    }
  },


  /**
   * 跳转页面
   */
  {
    path: '/redirectedPage',
    name: 'redirected',
    component: () => import('@/views/error/redirected.vue'),
    meta: {
      zIndex: 0
    }
  },
  /**
   * 无权限
   */
  {
    path: '/permission',
    name: 'permission',
    component: () => import('@/views/error/noPermissions.vue'),
    meta: {
      zIndex: 0
    }
  },
  /**
   * 异常地址
   */
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/error/404.vue'),
    meta: {
      zIndex: 0
    }
  },
  // 404 page must be placed at the end !!!
  { path: '/:cathAll(.*)', redirect: '/404' }
]













const vueRouter = createRouter({
  history: createWebHashHistory(),
  routes
})

vueRouter.beforeEach((to: any, from: any, next: Function) => {
  if (!excludesRouterName.includes(to.name)) {
    const _localStorage = window.parent.localStorage || localStorage;
    const tokenString = _localStorage.getItem(tokenName)
    if (!tokenString) {
      next({ name: 'login', params: { redirect: to.name } })
      return false
    }
    const userInfo = JSON.stringify(tokenString)
    if (!userInfo) {
      next({ name: 'login', params: { redirect: to.name } })
      return false
    }
    // if (!checkName(to)) {
    //   next({ name: 'permission', params: { redirect: to.name } })
    //   return false
    // }
  }
  next()
  return true
})

/**
 * 验证请求路由是否存在
 * @param to 
 * @returns 
 */
const checkName = function (to: any, roleUserMenuNameList: string[]) {
  // return true;
  if (!roleUserMenuNameList || roleUserMenuNameList.length <= 0) {
    return false;
  }
  return roleUserMenuNameList.includes(to.name) || excludesRouterName.includes(to.name);
}


export const checkRouteName = checkName;

export const pathRouter = routes

export default vueRouter
